package com.zndroid.netmonitor.monitor.impl;

import android.content.Context;
import android.os.Build;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.zndroid.netmonitor.NetType;
import com.zndroid.netmonitor.monitor.INetChangeCallBack;

/**
 * API >=23 采用广播的形式实现
 * */
@RequiresApi(api = Build.VERSION_CODES.M)
public class MonitorImplAPI23 extends MonitorImplAPI21 {

    public MonitorImplAPI23(@NonNull INetChangeCallBack netChangeCallBack) {
        super(netChangeCallBack);
    }

    @Override
    protected void monitorPhoneSignal(@NonNull Context context) {
        mPhoneStateListener = new PhoneStateListener() {
            @Override
            public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                super.onSignalStrengthsChanged(signalStrength);
                if (util.getNetType(context).contentEquals(NetType.NET_WIFI)) return;
                netChangeCallBack.onNetChanged(util.isNetworkAvailable(context), util.getNetType(context), signalStrength.getLevel());
            }
        };


        if (util.hasSimCard(context)) {
            TelephonyManager telephonyManager = (TelephonyManager) this.context.get().getSystemService(Context.TELEPHONY_SERVICE);
            assert telephonyManager != null;
            telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        }
    }
}
